package com.gidgabs;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.gidgabs.common.utils.HuanXinUtil;
import com.gidgabs.common.utils.PaypalUtil;
import com.gidgabs.config.GidgabsContants;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.gidgabs.config.HeadersCORSFilter;
import com.paypal.base.rest.APIContext;
import io.swagger.annotations.Api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.text.SimpleDateFormat;
import java.util.Date;

@Import(FdfsClientConfig.class)
@SpringBootApplication
@EnableSwagger2
@EnableScheduling
public class GidgabsApplication extends SpringBootServletInitializer {

    public GidgabsApplication() {
        setRegisterErrorPageFilter(false);
    }

    public static void main(String[] args) {
        SpringApplication.run(GidgabsApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(GidgabsApplication.class);
    }


    @Bean
    @Profile("dev")
    public APIContext apiContext_dev(){
        //return new APIContext("AdB3TDaVyIGsTHndOcfe0OZkiRIOWujZptnYPjDypPbBnnKrZsjNxtWbR8SyCmjUNVaJD-_vt5-ZzkRP","EK8oNHwauNn-DT7FOykZQAYRQbuQnQFGOPXiGv_gN3iXUUei4tSVNoYS6uiypWAU-TjkTBH-GXHN5EEB", "sandbox");
        return new APIContext(GidgabsContants.PAY_PAL_clientId_SAND_BOX,GidgabsContants.PAY_PAL_clientSecret_SAND_BOX,"sandbox");
    }
    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper=new ObjectMapper();
        //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(sdf);
        return objectMapper;
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    @Bean
    public HuanXinUtil huanXinUtil(RestTemplate restTemplate){
        return new HuanXinUtil(restTemplate);
    }
    @Bean
    @Profile("pro")
    public APIContext apiContext_pro(){
        //return new APIContext("AdB3TDaVyIGsTHndOcfe0OZkiRIOWujZptnYPjDypPbBnnKrZsjNxtWbR8SyCmjUNVaJD-_vt5-ZzkRP","EK8oNHwauNn-DT7FOykZQAYRQbuQnQFGOPXiGv_gN3iXUUei4tSVNoYS6uiypWAU-TjkTBH-GXHN5EEB", "sandbox");
        return new APIContext(GidgabsContants.PAY_PAL_clientId_LIVE,GidgabsContants.PAY_PAL_clientSecret_LIVE,"live");
    }
    @Bean
    public PaypalUtil paypalUtil(RedisTemplate<String, String> redisTemplate, APIContext apiContext){
        return new PaypalUtil(apiContext,redisTemplate);
    }
    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new HeadersCORSFilter());
        registration.addUrlPatterns("/*");
        registration.setName("HeadersCORSFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("英国gidgabs项目RESTful APIs文档")
                .description("英国gidgabs项目RESTful APIs文档")
                .contact("zhiyiyou")
                .version("1.0")
                .build();
    }
}
